package com.zhc.dejingapi.utils.fcmoroutingmain;


import com.zhc.dejingapi.mapper.QingAppfieldsMapper;
import com.zhc.dejingapi.mapper.QingAppfieldtablevaluesMapper;
import com.zhc.dejingapi.pojo.param.QingCommonBackResult;
import com.zhc.dejingapi.pojo.param.QingFcmoroutingDetailParam;
import com.zhc.dejingapi.pojo.param.QingFcmoroutingMainParam;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.QingAddDataResultMain;
import com.zhc.dejingapi.pojo.qingflow.queryresult.QingQueryActionResultBase;
import com.zhc.dejingapi.utils.qing.FCMoRoutingUploadUtils;
import com.zhc.dejingapi.utils.qing.QingflowAPIUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class QingFcMoroutingMainReportInjectUtils {

    public static QingCommonBackResult uploadInjectReportJob(QingFcmoroutingMainParam param,
                                                 QingflowApiInfo qqingflowApiInfo,
                                                 QingAppfieldsMapper qingAppfieldsMapper,
                                                 QingAppfieldtablevaluesMapper qingAppfieldtablevaluesMapper,
                                                 JdbcTemplate mysqlJdbcTemplate,
                                                             String strTableName){

        QingCommonBackResult commonBackResult=new QingCommonBackResult();
        String strappKey=param.getAppKey();


        if(param.getQingFcmoroutingdetail()==null||param.getQingFcmoroutingdetail().isEmpty()){

            List<QingFcmoroutingDetailParam> listBarCode=new ArrayList<>();
            //要计算结果
            commonBackResult.setErrCode(-10045);
            commonBackResult.setErrMsg("未提交条码内容！");
            return commonBackResult;
        }

        //新增的时候

        //String strTableName="fcmoroutingInject";//虚拟表名，不作回写，需要修改

        boolean bSuccess=false;

        Map<String,String> mapContrastFields=new HashMap<>();

        //region mapContrastFields&mapContrastDetailFields
        mapContrastFields.put("生产任务单号","workTaskCode");//生产任务单号 workTaskCode
        mapContrastFields.put("产品编号","invCode");//产品编号 invCode
        mapContrastFields.put("产品名称","invName");//产品名称 invName
        mapContrastFields.put("规格型号","invStd");//规格型号 invStd
        mapContrastFields.put("计划产量","planQty");//计划产量 planQty
        mapContrastFields.put("工序名称","opName");//工序名称 opName
        mapContrastFields.put("批次号","proBatchNo");//批次号 proBatchNo
        mapContrastFields.put("设备编号","eqCode");//设备编号 eqCode
        mapContrastFields.put("模具编号","moldCode");//模具编号 moldCode
        mapContrastFields.put("班次","workShiftCode");//班次 workShiftCode
        mapContrastFields.put("生产人员","psnName");//生产人员 psnName
        mapContrastFields.put("员工工号","psnCode");//员工工号 psnCode
        mapContrastFields.put("报工开始时间","reportJobStartTime");//报工开始时间 reportJobStartTime
        mapContrastFields.put("报工结束时间","reportJobEndTime");//报工结束时间 reportJobEndTime
        mapContrastFields.put("报工方式","reportType");//报工方式 reportType
        mapContrastFields.put("预计良品数","planQualfyQty");//预计良品数 planQualfyQty
        mapContrastFields.put("报工后良品数","afterQualifyQty");//报工后良品数 afterQualifyQty
        mapContrastFields.put("预计箱数","planNum");//预计箱数 planNum
        mapContrastFields.put("剩余未清数量","leaveQty");//剩余未清数量 leaveQty
        mapContrastFields.put("报工后剩余未清数","afterLeaveQty");//报工后剩余未清数 afterLeaveQty
        mapContrastFields.put("单箱数量/件","qtyPerCarton");//单箱数量/件 qtyPerCarton
        mapContrastFields.put("良品数","qualifyQty");//良品数 qualifyQty
        mapContrastFields.put("箱数","proNum");//箱数 proNum
        mapContrastFields.put("尾箱数量/件","modQty");//尾箱数量/件 modQty
        mapContrastFields.put("机台合模数","eqRunMoldQty");//机台合模数 eqRunMoldQty
        mapContrastFields.put("报工时间","reportJobTime");//报工时间 reportJobTime
        mapContrastFields.put("客户代号","cusCode");//客户代号 cusCode
        mapContrastFields.put("报工批次时间","workproTmBatch");//报工批次时间 workproTmBatch


        Map<String,String> mapContrastDetailFields=new HashMap<>();

        mapContrastDetailFields.put("箱码","barcodeStr");//计划数  opQty
        mapContrastDetailFields.put("数量","iQty");//报工数配比  opRatio
        //endregion

        FCMoRoutingUploadUtils UploadUtils =null;
        try{
            UploadUtils = new FCMoRoutingUploadUtils(qqingflowApiInfo,
                    mysqlJdbcTemplate,
                    qingAppfieldsMapper,
                    qingAppfieldtablevaluesMapper,
                    mapContrastFields,
                    mapContrastDetailFields,
                    strTableName);
        }
        catch (Exception e){
            log.error(e.getMessage()+"\n"+e.getStackTrace());
            commonBackResult.setErrCode(-10049);
            commonBackResult.setErrMsg("创建提交对象出错："+e.getMessage()+"\n"+e.getStackTrace());
            return commonBackResult;
        }

        if(UploadUtils==null){
            commonBackResult.setErrCode(-10045);
            commonBackResult.setErrMsg("提交对象创建失败！");
            return commonBackResult;
        }

        try {

            //region 新增初始行

            try {

                QingAddDataResultMain resultMain = UploadUtils.AddEntity(param);
                //region 处理返回结果
                if (resultMain == null) {

                    commonBackResult.setErrCode(-10015);
                    commonBackResult.setErrMsg("服务器返回内容为空！");

                } else {
                    if (resultMain.getErrCode() == 0) {
                        commonBackResult.setErrCode(resultMain.getErrCode());
                        commonBackResult.setErrMsg("提交成功！");
                        commonBackResult.setBackValue(resultMain.getResult().getRequestId());
                        try{
                            Thread.sleep(500);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        //获取查询值的结果
                        String strRequestId=resultMain.getResult().getRequestId();

                        QingQueryActionResultBase actionResultBase= QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,strRequestId);
                        if (actionResultBase==null){
                            commonBackResult.setErrCode(-10066);
                            commonBackResult.setErrMsg("提交成功：获取提交结果的内容为空！");

                        }
                        else{
                            if (actionResultBase.getErrorCode()==0){

                                commonBackResult.setErrCode(0);
                                commonBackResult.setErrMsg("提交成功！");

                            }
                            else{

                                commonBackResult.setErrCode(actionResultBase.getErrorCode());
                                commonBackResult.setErrMsg(actionResultBase.getMessage());

                            }
                        }

                    } else {
                        commonBackResult.setErrCode(resultMain.getErrCode());
                        commonBackResult.setErrMsg(resultMain.getErrMsg());

                    }

                }
                //endregion
            } catch (Exception e) {
                e.printStackTrace();

                commonBackResult.setErrCode(-10076);
                commonBackResult.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());

            }

            //endregion

        }
        catch (Exception e){
            e.printStackTrace();
            commonBackResult.setErrCode(-10076);
            commonBackResult.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());

        }

        return commonBackResult;
    }
}
